<%@page import="com.nxu.entity.UserBean"%>
<%@page import="com.nxu.entity.BookBean"%>
<%@page import="com.nxu.biz.impl.BooksBizImpl"%>
<%@page import="com.nxu.biz.BooksBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%-- 用户权限检查 --%>
<%
    // 获取session中的用户信息
    UserBean user = (UserBean)session.getAttribute("user");
    
    // 管理员和商家用户重定向到对应页面
    if(user != null) {
        if(user.getUserType() == 1) {  // 管理员
            response.sendRedirect("manage.jsp");
            return;
        } else if(user.getUserType() == 3) {  // 商家
            response.sendRedirect("merchant.jsp");
            return;
        }
    }

    // 处理错误消息
    String error = request.getParameter("error");
    String errorMessage = "";
    if("book_unavailable".equals(error)) {
        errorMessage = "该商品已下架，无法购买";
    }

    // 获取图书信息
    String isbn = request.getParameter("isbn");
    BooksBiz biz = new BooksBizImpl();
    BookBean book = null;
    
    if (isbn != null && !isbn.isEmpty()) {
        book = biz.getBookByIsbn(isbn);
    }
%>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= book != null ? book.getTitle() : "商品详情" %></title>
    <style>
        /* 基础样式 */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            color: #333;
            line-height: 1.6;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .error-message {
            color: #dc3545;
            text-align: center;
            padding: 20px;
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 20px;
            background-color: #f8d7da;
            border-radius: 4px;
        }
        
        /* 图书详情区域 */
        .book-detail {
            display: flex;
            gap: 50px;
            align-items: flex-start;
        }
        
        .book-image {
            flex: 0 0 400px;
            background: #f9f9f9;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        
        .book-image img {
            width: 100%;
            height: auto;
            display: block;
        }
        
        .book-info {
            flex: 1;
        }
        
        .book-title {
            font-size: 28px;
            margin-bottom: 15px;
            color: #222;
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        /* 状态标签 */
        .status-badge {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 14px;
            font-weight: 500;
        }
        
        .status-active {
            background-color: #d4edda;
            color: #155724;
        }
        
        .status-inactive {
            background-color: #f8d7da;
            color: #721c24;
        }
        
        /* 信息表格 */
        .info-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        
        .info-table th, 
        .info-table td {
            padding: 12px 15px;
            border-bottom: 1px solid #eee;
            text-align: left;
        }
        
        .info-table th {
            width: 120px;
            font-weight: 600;
            color: #555;
        }
        
        /* 价格区域 */
        .price-section {
            margin: 25px 0;
        }
        
        .price-label {
            font-size: 16px;
            color: #666;
            margin-bottom: 5px;
        }
        
        .price {
            font-size: 28px;
            color: #e74c3c;
            font-weight: bold;
        }
        
        /* 按钮区域 */
        .action-buttons {
            display: flex;
            gap: 15px;
            margin-top: 30px;
        }
        
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .btn-primary {
            background-color: #28a745;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #218838;
            transform: translateY(-2px);
        }
        
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        
        .btn-secondary:hover {
            background-color: #5a6268;
            transform: translateY(-2px);
        }
        
        .btn-disabled {
            background-color: #e9ecef;
            color: #adb5bd;
            cursor: not-allowed;
        }
        
        /* 下架提示 */
        .unavailable-message {
            color: #dc3545;
            font-weight: 500;
            margin-top: 15px;
            padding: 10px;
            background-color: #f8d7da;
            border-radius: 4px;
        }
        
        /* 响应式设计 */
        @media (max-width: 992px) {
            .book-detail {
                flex-direction: column;
                gap: 30px;
            }
            
            .book-image {
                flex: 0 0 auto;
                width: 100%;
                max-width: 400px;
                margin: 0 auto;
            }
        }
        
        @media (max-width: 576px) {
            .container {
                padding: 20px;
            }
            
            .book-title {
                font-size: 24px;
                flex-direction: column;
                align-items: flex-start;
                gap: 8px;
            }
            
            .action-buttons {
                flex-direction: column;
            }
            
            .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <%-- 显示错误消息 --%>
        <% if(!errorMessage.isEmpty()) { %>
            <div class="error-message"><%= errorMessage %></div>
        <% } %>
        
        <%-- 图书不存在时的提示 --%>
        <% if(book == null) { %>
            <div class="error-message">
                <%= isbn == null || isbn.isEmpty() ? "ISBN参数无效" : "未找到该图书信息" %>
            </div>
        <% } else { %>
            <%-- 图书详情内容 --%>
            <div class="book-detail">
                <%-- 图书图片 --%>
                <div class="book-image">
                    <img src="images/<%= book.getImagefile() %>" 
                         alt="<%= book.getTitle() %>"
                         onerror="this.src='images/default-book.jpg'">
                </div>
                
                <%-- 图书信息 --%>
                <div class="book-info">
                    <%-- 标题和状态 --%>
                    <h1 class="book-title">
                        <%= book.getTitle() %>
                        <span class="status-badge <%= book.isActive() ? "status-active" : "status-inactive" %>">
                            <%= book.isActive() ? "在售" : "已下架" %>
                        </span>
                    </h1>
                    
                    <%-- 详细信息表格 --%>
                    <table class="info-table">
                        <tr>
                            <th>ISBN</th>
                            <td><%= book.getIsbn() %></td>
                        </tr>
                        <tr>
                            <th>版权信息</th>
                            <td><%= book.getCopyright() %></td>
                        </tr>
                        <tr>
                            <th>版次</th>
                            <td>第 <%= book.getEditionnumber() %> 版</td>
                        </tr>
                        <tr>
                            <th>出版社</th>
                            <td><%= book.getPublisherid() %></td>
                        </tr>
                    </table>
                    
                    <%-- 价格 --%>
                    <div class="price-section">
                        <div class="price-label">价格</div>
                        <div class="price">￥<%= String.format("%.2f", book.getPrice()) %></div>
                    </div>
                    
                    <%-- 操作按钮 --%>
                    <div class="action-buttons">
                        <% if(book.isActive()) { %>
                            <button class="btn btn-primary" onclick="addToCart('<%= book.getIsbn() %>')">
                                加入购物车
                            </button>
                        <% } else { %>
                            <button class="btn btn-disabled" disabled>
                                商品已下架
                            </button>
                        <% } %>
                        <button class="btn btn-secondary" onclick="viewCart()">
                            查看购物车
                        </button>
                    </div>
                    
                    <%-- 下架提示 --%>
                    <% if(!book.isActive()) { %>
                        <div class="unavailable-message">
                            该商品已下架，暂时无法购买
                        </div>
                    <% } %>
                </div>
            </div>
        <% } %>
    </div>
    
    <script>
        // 加入购物车函数
        function addToCart(isbn) {
            window.location.href = "addToCart.jsp?isbn=" + encodeURIComponent(isbn);
        }
        
        // 查看购物车函数
        function viewCart() {
            window.location.href = "viewCart.jsp";
        }
    </script>
</body>
</html>